package hlhj.fhp.newslib.activitys

import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.widget.Toast
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.model.Response
import hlhj.fhp.newslib.BaseAty
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.adapters.CatMultiAdp
import hlhj.fhp.newslib.javabean.CatBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.network.CustomAction
import hlhj.fhp.newslib.network.LoadingAction
import hlhj.fhp.newslib.utils.ToastUtil
import kotlinx.android.synthetic.main.aty_search.*
import kotlinx.android.synthetic.main.hlhj_news_list_lo.*
import kotlinx.android.synthetic.main.hlhj_news_lo_comment.*
import java.util.ArrayList

/**
 * Created by Administrator on 2018\6\26 0026.
 */
class SearchAty :BaseAty(), SpringView.OnFreshListener {
    private val catDatas=ArrayList<CatBean.DataBean.ListBeanX>()
    private lateinit var mulitAdp:CatMultiAdp
    private var page=1;
    override fun initListener() {
        btSearch.setOnClickListener {
            if (etSearch.text.isEmpty()){
                ToastUtil.toast("搜索内容不能为空")
                return@setOnClickListener
            }
            catDatas.clear()
            search()
        }
        btExit.setOnClickListener {
            finish()
        }
    }

    override fun initView() {
        /*Head*/
        spView.header=DefaultHeader(this)
        spView.footer=DefaultFooter(this)
        spView.setListener(this)
        mulitAdp= CatMultiAdp(catDatas,this)

        /*EmptyView*/
        val emptyView = LayoutInflater.from(this).inflate(R.layout.search_empty, null)

        recyclerview.adapter=mulitAdp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        mulitAdp.setEmptyView(emptyView)



    }

    override fun getContentId(): Int {
        return R.layout.aty_search
    }

    private fun search(){
        Apis.serach(etSearch.text.toString(),page)
                .doOnSubscribe(LoadingAction(this))
                .subscribe(object :CustomAction<Response<CatBean>>{
                    override fun onResult(t: Response<CatBean>) {
                        val catBean = t.body()
                        loadingDialog.dismiss()
                        spView.onFinishFreshAndLoad()
                        catDatas.addAll(catBean.data.list)
                        mulitAdp.notifyDataSetChanged()
                    }
                })
    }

    override fun onLoadmore() {
        page++
        search()
    }

    override fun onRefresh() {
        page=1
        search()
    }
}

